package com.education.basic.action;

import javax.annotation.Resource;

import com.education.basic.entity.Administrator;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.education.basic.ao.LoginAo;
import com.yunshangwoniu.core.ValidateCodeService.ValidateCodeService;
import com.yunshangwoniu.core.baseAction.BaseAction;
import com.yunshangwoniu.core.cache.CacheAble;
import com.yunshangwoniu.core.utils.RequestUtils;

@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage(value="struts-default")
public class BasicAction extends BaseAction
{
	private static final long serialVersionUID = 6935088116705918969L;
	@Resource
	private ValidateCodeService validateCodeService;
	@Resource
	private LoginAo loginAo;
	@Resource
	private CacheAble<Object, Object> cache;
    @Resource
    private CacheAble<Object, Object> cacheAble;
	
	private String validateCode;
	private String username;
	private String password;
	private String loginMsg;
    private Administrator ad;
	
	@Action(value="index",results={@Result(name="success",location="login.jsp")})
	public String index()
	{
		validateCode = validateCodeService.getValidateCode(this.getRequest());
		
		return SUCCESS;
	}
	
	@Action(value="login",results={@Result(name="success",location="desktop.htm",type="redirectAction"),@Result(name="error",location="login.jsp")})
	public String login()
	{
		LoginMsg msg = new LoginMsg("");
		String result = loginAo.login(username, password, validateCode, msg, getRequest(), getResponse());
		loginMsg = msg.getLoginMsg();
		if("error".equals(result))validateCode = validateCodeService.getValidateCode(this.getRequest());
		
		return result;
	}

    @Action(value="logout",results={@Result(name="success",location="index.htm",type="redirectAction")})
    public String logout()
    {
        loginAo.loginOut(null, getRequest(), null);
        return SUCCESS;
    }

	@Action(value="desktop",results={@Result(name="success",location="desktop.jsp"),@Result(name="error",location="index.htm",type="redirectAction")})
	public String desktop()
	{
		if(isLogin(cache, getRequest(), "username",RequestUtils.getIp(getRequest())))
		{
            ad = (Administrator) cacheAble.get(RequestUtils.getIp(getRequest()));
			return SUCCESS;
		}else
		{
			return ERROR;
		}
	}
	
	public ValidateCodeService getValidateCodeService()
	{
		return validateCodeService;
	}

	public void setValidateCodeService(ValidateCodeService validateCodeService)
	{
		this.validateCodeService = validateCodeService;
	}

	public String getValidateCode()
	{
		return validateCode;
	}

	public void setValidateCode(String validateCode)
	{
		this.validateCode = validateCode;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}
	public String getLoginMsg()
	{
		return loginMsg;
	}
	public void setLoginMsg(String loginMsg)
	{
		this.loginMsg = loginMsg;
	}
	
	public class LoginMsg
	{
		private String loginMsg;
		public LoginMsg(String loginMsg)
		{
			this.loginMsg = loginMsg;
		}
		public String getLoginMsg()
		{
			return loginMsg;
		}
		
		public void setLoginMsg(String loginMsg)
		{
			this.loginMsg = loginMsg;
		}
	}

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public LoginAo getLoginAo() {
        return loginAo;
    }

    public void setLoginAo(LoginAo loginAo) {
        this.loginAo = loginAo;
    }

    public CacheAble<Object, Object> getCache() {
        return cache;
    }

    public void setCache(CacheAble<Object, Object> cache) {
        this.cache = cache;
    }

    public CacheAble<Object, Object> getCacheAble()
	{
		return cacheAble;
	}

	public void setCacheAble(CacheAble<Object, Object> cacheAble)
	{
		this.cacheAble = cacheAble;
	}

	public Administrator getAd() {
        return ad;
    }

    public void setAd(Administrator ad) {
        this.ad = ad;
    }
}
